package com.ptrsystem.siatd.controllers;

import com.ptrsystem.siatd.beans.GenDocPersonal;
import com.ptrsystem.siatd.dao.logica.ConsultasTdLogica;
import com.ptrsystem.siatd.dao.logica.MantenimientoAnnioLogica;
import com.ptrsystem.siatd.dao.logica.MantenimientoDocumentoLogica;
import com.ptrsystem.siatd.dao.logica.MantenimientoEstadoDocPersonaLogica;
import com.ptrsystem.siatd.dao.logica.MantenimientoGenDocPersonalLogica;
import com.ptrsystem.siatd.dao.logica.MantenimientoTipoAnioLogica;
import com.ptrsystem.siatd.dao.logica.MantenimientoUnidDepartIntLogica;
import com.ptrsystem.siatd.dao.logica.MantenimientoUnidPadreInternoLogica;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Formatter;
import java.util.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class GenDocPersonalController {
    
    @Autowired
    private MantenimientoGenDocPersonalLogica logica;
    @Autowired
    private MantenimientoAnnioLogica anio;
    @Autowired
    private MantenimientoTipoAnioLogica tipAnio;
    @Autowired
    private MantenimientoEstadoDocPersonaLogica estDocPer;
    @Autowired
    private MantenimientoUnidDepartIntLogica uniDep;
    @Autowired
    private MantenimientoUnidPadreInternoLogica uniPad;
    @Autowired
    private MantenimientoDocumentoLogica doc;
    @Autowired
    private ConsultasTdLogica consultas;
    
    @RequestMapping(value = "registrarGenDocPersonal.htm", method = RequestMethod.GET)
    private String registrarGenDocPer(HttpServletRequest req) {
        req.getSession(true).setAttribute("result", "Estado De Mantenimiento");
        req.getSession(true).setAttribute("nroEmidocAdm", logica.getGeneraCodGenDocPers());
        req.getSession(true).setAttribute("ani", anio.listaAnio());
        req.getSession(true).setAttribute("tipAni", tipAnio.listaTipoAnio());
        req.getSession(true).setAttribute("estCodPer", estDocPer.listaEstDocPersona());
        req.getSession(true).setAttribute("uniDep", uniDep.listaUniDepInt());
        req.getSession(true).setAttribute("uniPad", uniPad.listaUnidadPadre());
        req.getSession(true).setAttribute("doc", doc.listaDocumento());
        req.getSession(true).setAttribute("iniDat", consultas.getIniDatosUsuario(String.valueOf(req.getSession(true).getAttribute("dni"))));
        return "Mantenimiento/NRegGenDocPersonal";
    }
    
    @RequestMapping(value = "registraGenDocPersonal.htm", method = RequestMethod.POST)
    private String registraGenDocPer(GenDocPersonal docPer, HttpServletRequest req) {
        Formatter numF = new Formatter();
        int num = logica.generaNroDocPers(docPer.getCodTipoDoc());
        numF.format("%06d", num);
        docPer.setNroEmisionDocAdm(String.valueOf(logica.getGeneraCodGenDocPers()));
        docPer.setNroTipoDocPers(String.valueOf(numF));
        boolean rs = logica.registraGenDocPers(docPer);
        if (rs) {
            req.getSession(true).setAttribute("result", "Registro Efectuado Correctamente");
            req.getSession(true).setAttribute("nroEmidocAdm", logica.getGeneraCodGenDocPers());
            
        } else {
            req.getSession(true).setAttribute("result", "Registro No Efectuado");
            
        }
        return "Mantenimiento/NRegGenDocPersonal";
    }
    
    @RequestMapping(value = "bandejaEntradaGenDocPersonal.htm", method = RequestMethod.POST)
    private void bandejaEntradaGenDocPersonal(HttpServletResponse res, HttpServletRequest req) throws IOException {
        PrintWriter out = res.getWriter();
        String rs = "";
        List<Map> lista = logica.getDocBandejaEntrada(req.getParameter("dniDes"),
                req.getParameter("asu"), req.getParameter("fec"), req.getParameter("uniPad"),
                req.getParameter("uniDep"), req.getParameter("priApe"), req.getParameter("segApe"), req.getParameter("preNom"));
        for (Map lista1 : lista) {
            rs += "<tr>"
                    + "<td>" + lista1.get("rem") + "</td>"
                    + "<td>" + lista1.get("tipDoc") + "</td>"
                    + "<td>" + lista1.get("numDoc") + "</td>"
                    + "<td>" + lista1.get("nomDoc") + "</td>"
                    + "<td>" + lista1.get("asu") + "</td>"
                    + "<td>" + lista1.get("fec") + "</td>"
                    + "<td>" + lista1.get("uniPad") + "</td>"
                    + "<td>" + lista1.get("uniDep") + "</td>"
                    + "<td>" + lista1.get("ani") + "</td>"
                    + "<td>" + lista1.get("est") + "</td>"
                    + "<td><a href='" + lista1.get("rut") + "'>Ver Doc</a></td>"
                    + "</tr>";
        }
        out.print(rs);
        out.close();
    }
    
    @RequestMapping(value = "generaNroGenDocPersonal.htm", method = RequestMethod.POST)
    private void generaNroDocPersonal(HttpServletResponse res, @RequestParam("codTipDoc") String codTipDoc) throws IOException {
        PrintWriter out = res.getWriter();
        int num = logica.generaNroDocPers(codTipDoc);
        Formatter numF = new Formatter();
        numF.format("%06d", num);
        out.print(numF);
        out.close();
    }
}
